package com.jackson.dao;

import com.jackson.entity.StudentEntity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/*
 * Author: JacksonCoder
 * FileName: StudentDao
 * Version: 1.0.0
 */
public class StudentDao {
    private final String url = "jdbc:mysql://localhost:3306/learnsql";
    private final String user = "root";
    private final String password = "123456";
    /**
     * 查询学生信息
     */
    public ArrayList<StudentEntity> allStudent(){
        ArrayList<StudentEntity> studentEntities = new ArrayList<>();
        try (Connection conn = DriverManager.getConnection(url, user, password);
             Statement stmt = conn.createStatement()) {

            String query = "select * from student_info";
            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {
                // 获取该行数据的第一列 id
                Integer id = rs.getInt("id");
                // 获取该行数据的第二列 name
                String name = rs.getString("name");
                // 获取该行数据的第三列 sex
                String sex = rs.getString("sex");
                // 获取该行数据的第四列 chinese
                Integer chinese = rs.getInt("chinese");
                // 获取该行数据的第四列 english
                Integer english = rs.getInt("english");
                // 获取该行数据的第四列 maths
                Integer maths = rs.getInt("maths");

                // 将db中查询到数据封装成java学生对象
                StudentEntity studentEntity = new StudentEntity(id, name, sex, chinese,english,maths);
                // 将该对象存入到集合中
                studentEntities.add(studentEntity);
//                return studentEntities;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return studentEntities;
    }
}
/**
 * String result = "number: " + rs.getInt("number") +
 *                         " name: " + rs.getString("name") +
 *                         " hiredate: " + rs.getDate("hiredate") +
 *                         "salary: " + rs.getDouble("salary") +
 *                         "bonus: " + rs.getDouble("bonus") +
 *                         "department: " + rs.getString("department");
 *                 System.out.println(result);
 */